<?php
require_once('../../core.inc.php');
require_once('Model/SiteManage.php');
require_once('View/Smarty.php');
require_once('Model/DB.php');
require_once('Model/Validate.php');
require_once('Model/Pager.php');
require_once('Plugin/Role.php');
require_once('Plugin/Site.php');
require_once('Plugin/Staff.php');

$sys = new Model_SiteManage();
$sys->checkLogin();
$params = $_REQUEST;
$smarty = new View_Smarty($params);
$role  = new Model_Role();

define('MANAGE_STAFF', 1000 );// スタッフ管理
if( !in_array( MANAGE_STAFF, $_SESSION['ADMIN_LOGIN']['SITE'][ $_SESSION['DASH_BOARD']['SITE_ID'] ] ) ) exit;

//サイト一覧取得
$site = new Model_Site();
foreach($site->getList($data=array()) as $id=>$one)
{
    $site_list[$id] = $one["site_name"];
}
$smarty->assign('site_list', $site_list);


if (isset($params['do']) && function_exists('exec_' . $params['do'])){
   $params['op'] = call_user_func('exec_' . $params['do'], &$params, &$smarty, &$role);
}
if (isset($params['op']) && function_exists('op_' . $params['op'])){
    call_user_func('op_' . $params['op'], $params, &$smarty, &$role);
}
else{
    call_user_func('op_search', $params, &$smarty, &$role);
}


function op_search($params, &$smarty, &$role)
{
    if (isset($params['search'])){
        $max_row = 20;
        $pager   = new Model_Pager();
        $page_id = isset($params['page_id']) && $params['page_id'] > 1 ? $params['page_id'] : 1;
        $pager->getPagerInfo($role->getListCount($params['search']), $page_id, $max_row);
        $list = $role->getList($params['search'], 'id asc', $max_row, ($max_row * ($page_id - 1)));
    //var_dump($list);exit;
        $smarty->assign('list', $list);
        $smarty->assign('pager', $pager);
        $smarty->assign('forms', $params['search']);
    }
    
    $smarty->display('staff/staff_role.html');
}

// 新規登録
function op_regist($params, &$smarty, &$role)
{
    if (!isset($params['forms'])){
        $forms = array();
        foreach($role->getFormKeys() as $key){
            $forms[$key] = '';
        }
        $params['forms'] = $forms;
    }
    
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('staff/staff_roleform.html');
}

function exec_validate_regist_form(&$params, &$smarty, &$role)
{
    $err = array();

    if ($role->validate($params['forms'], $err)){//権限はヴァリデーションしていない
        return exec_regist_form($params, $smarty, $role);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
//print_r($err);exit;
    return 'regist';
}
function exec_regist_form(&$params, &$smarty, &$role)
{
    $id = $role->insert($params['forms']);
    
    // 権限    
    if(isset($params['forms_auth']['auth']))
        auth_register($id, $params['forms_auth']['auth']);
    else
        auth_register($id);//全て削除
                                
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=regist_complete&id=' . $id);
    exit();
}

function op_regist_complete($params, &$smarty, &$role)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
        $smarty->assign('forms', array('id' => $params['id']));
    }
    $smarty->assign('message', '登録が完了しました');
    $smarty->display('staff/staff_roleform.html');
}

// 編集
function op_edit($params, &$smarty, &$role)
{
    if (!isset($params['id'])){
        __raise_error();
    }

    $db = new Model_DB();
    $id = $params['id'];
    if (!isset($params['forms'])){
        $forms = $role->get($id);
        $params['forms']   = $forms;
        
        $sql = sprintf("select * from D_ROLE_AUTH where role_id = '" . $db->escape($id) ."'");
        $db->query($sql);
        while($row = $db->fetch())
        {
            $params['forms_auth']['site_id'] = $row['site_id'];
            $params['forms_auth']['auth_mode'] = $row['auth_mode'];
            $params['forms_auth']['role'] = $row['role'];
            $params['forms_auth']['auth'][] = $row['auth_cd'];
        }
    }
    
    $staff = new Model_Staff();
    $arr_auth = $staff->getAuth();

    $db->query(sprintf("SELECT * FROM D_SITE_PLUGIN WHERE site_id = '%u' ORDER BY auth_cd ASC", (int)$params['forms']['site_id']));
    $plugin = array();
    while($res = $db->fetch()){
            $cd = $res['auth_cd'];
            $img_file_name = explode(' ', strtolower($arr_auth[$cd]));
            $tmp = array('value' => $cd, 'name' => $arr_auth[$cd], 'img_file_name' => $img_file_name[0]);
            if(isset($params['forms_auth']['auth']) and in_array($cd, $params['forms_auth']['auth']))
            {
                $tmp['checked'] = '1';
            }
            $plugin[] = $tmp;
    }

    $smarty->assign('plugin', $plugin);
    $smarty->assign('id', $id);
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->assign('forms_auth', $params['forms_auth']);
    $smarty->display('staff/staff_roleform.html');
}

function exec_validate_update_form(&$params, &$smarty, &$role)
{
    if (!isset($params['id'])){
        __raise_error();
    }else{
       $params['forms']['id'] = $params['id'];
    }
    
    $err = array();
    
    if ($role->validate($params['forms'], $err)){
        return exec_update_form($params, $smarty, $role);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'edit';
}
function exec_update_form(&$params, &$smarty, &$role)
{
    $id = $params['id'];
    
    if($_POST['mode'] == "delete")
    {
        $params['forms']['enabled'] = 0;
    }
	
    $role->update($id, $params['forms']);
    
    //権限
    if(isset($params['forms_auth']['auth']))
        auth_register($id, $params['forms_auth']['auth']);
    else
        auth_register($id);//全て削除
    
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=edit_complete');
    exit();
}
function op_edit_complete($params, &$smarty, &$role)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
        $smarty->assign('forms', array('id' => $params['id']));
    }
    $smarty->assign('message', '更新が完了しました');
    $smarty->display('staff/staff_roleform.html');
}

// 権限登録・更新
function auth_register($role_id, $auth = '')
{
    $db = new Model_DB();
    $sql = sprintf( "DELETE FROM D_ROLE_AUTH WHERE role_id = %u", $role_id );
    $db->query( $sql );

    if($auth != '')
    {
        foreach( $auth as $k => $v ){
            $sql = sprintf( "INSERT INTO D_ROLE_AUTH( role_id, auth_cd ) VALUES( %u, '$v')",
                    $role_id,
                    $db->escape($v)
            );
            $db->query( $sql );
        }
    }
}
?>